package com.sx.first.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.sx.first.dao.DepartmentDAO;
import com.sx.first.model.Department;

@Repository("departmentDAO")
@Scope("prototype")
public class DepartmentDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<Department> findAll() {
		@SuppressWarnings("unchecked")
		List<Department> list = sessionFactory.getCurrentSession().createQuery("from Department").list();
		return list;
	}

	public void save(Department dep) {
		sessionFactory.getCurrentSession().save(dep);
	}

	public void delete(Department dep) {
		sessionFactory.getCurrentSession().delete(dep);
	}

	public void update(Department dep) {
		sessionFactory.getCurrentSession().update(dep);
	}

	public Department findById(int depId) {
		return sessionFactory.getCurrentSession().get(Department.class, depId);
	}

}
